package com.example.neteasemusic.welcome

import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.annotation.RequiresApi
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.Glide
import com.example.lib_base.manager.AdvManager
import com.example.lib_base.manager.SongManager
import com.example.neteasemusic.R
import com.example.neteasemusic.databinding.ActivityWelcomeBinding
import com.example.neteasemusic.recyclerView.PublicViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class WelcomeActivity : AppCompatActivity() {
    private val viewModel :PublicViewModel by viewModels()
    private lateinit var binding:ActivityWelcomeBinding

    @RequiresApi(Build.VERSION_CODES.R)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityWelcomeBinding.inflate(layoutInflater)

        setContentView(binding.root)

        //加载广告资源
        lifecycleScope.launch (Dispatchers.IO){
            viewModel.advManager.loadAdv{
                if(!it) return@loadAdv

                //获取即将显示的图片的url
                val adv = AdvManager.getInstance().getAdv() ?: return@loadAdv
                //提前下载url对应的图片
                //预加载
                Glide.with(this@WelcomeActivity).load(adv.uri).preload()
            }

            //加载歌曲资源
            viewModel.songManager.requireData{result,list->

                //TODO 测试==========================================
                SongManager.getInstance()?.songs?.forEach{song->
                    song.album = "https://p2.music.126.net/rYmW2dKFseH2GYcVk8Lrqw==/109951168737879331.jpg?param=130y130"
                    song.url = "https://blog-img-bitebyte.oss-cn-chengdu.aliyuncs.com/audio/%E3%83%8A%E3%83%84%E3%83%8E%E3%82%BB%20_%20%E7%BC%B6%E7%BC%B6%20%28KANKAN%29%20-%20escape%20%28feat_%20%E7%BC%B6%E7%BC%B6%29.mp3"
                }
                //TODO ==============================================


                //TODO 测试
               /* if(result){
                    Log.v("wjb","yes")
                }else{
                    Log.v("wjb","no")
                }*/
            }
        }







    }
}